package com.ljd.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.hosp.service.HospitalSetService;
import com.ljd.yygh.exception.YyghException;
import com.ljd.yygh.model.hosp.Hospital;
import com.ljd.yygh.model.hosp.HospitalSet;
import com.ljd.yygh.result.Result;
import com.ljd.yygh.util.MD5;
import com.ljd.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.List;
import java.util.Objects;
import java.util.Random;
@CrossOrigin
@Api(tags = "后台医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;
    @ApiOperation("医院设置列表")
    @GetMapping("findAll")
    public Result findAll() {
        List<HospitalSet> hospitalSets = hospitalSetService.list();
        return Result.ok(hospitalSets);
    }
    @ApiOperation("逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result remove(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        if(b) {
            return Result.ok();
        }
        return Result.fail();
    }
    @ApiOperation("分页查询带条件")
    @PostMapping("PageHospitalSet/{current}/{limit}")
    public Result findPageHospitalSet(@PathVariable("current") Long current,
                                      @PathVariable("limit") Long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo vo) { // 使用 @RequestBody则以json格式封装请求体，不加的话就是键值对的方式携带参数
        Page<HospitalSet> page = hospitalSetService.findPageHospitalSet(current,limit,vo);
        return Result.ok(page);
    }
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态
        hospitalSet.setStatus(1);
        // 设置签名密钥
        String s = MD5.encrypt(System.currentTimeMillis()+""+new Random().nextInt(1000));
        hospitalSet.setSignKey(s);
        boolean b = hospitalSetService.save(hospitalSet);
        if(b) {
            return Result.ok();
        }
        return Result.fail();
    }
    @ApiOperation("根据查询ID医院设置")
    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    @ApiOperation("根据ID修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if(b) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("批量逻辑删除")
    @DeleteMapping("deleteBatchHospitalSet")
    public Result deleteBatchHospitalSet(@RequestBody List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);
        if(b) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        // 根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 为空返回失败
        if(hospitalSet==null) {
            throw new YyghException("程序出错",520);
        }
        Integer hospitalSetStatus = hospitalSet.getStatus();
        // 获取状态，当状态与参数中的状态不等时，进行修改
        if(!Objects.equals(hospitalSetStatus,status)) {
            hospitalSet.setStatus(status);
            hospitalSetService.updateById(hospitalSet);
        }
        return Result.ok();
    }
    @ApiOperation("发送签名")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet==null) {
            throw new YyghException("程序出错",520);
        }
        String signKey = hospitalSet.getSignKey();
        return Result.ok();
    }
}
